const mysql = require('mysql')
const axios = require('axios')
const cheerio = require('cheerio')

const page = 1
const count = 1

const options = {
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: '',
  database: 'myBooks'
}

const conn = mysql.createConnection(options)

conn.connect((err) => {
  if (err) {
    console.log(err)
  }
  console.log('数据库连接成功')
})

async function getPageUrl(num) {
  const httpURL = 'https://xxx/page/' + num
  const res = await axios.get(httpURL)
  // console.log(res.data)
  const $ = cheerio.load(res.data)
  $('#cardslist .card-item .thumb-img>a').each((index, ele) => {
    let href = $(ele).attr('href')
    // console.log(index + ' - ' + href)
    getBookInfo(href)
  })
}

async function getBookInfo(href) {
  const res = await axios.get(href)
  const $ = cheerio.load(res.data)
  
  const book_img = $('.article-content .bookpic img').attr('src')
  
  let book_name = $('.article-content .bookinfo li:nth-child(1)').text()
  book_name = book_name.substring(3, this.length)
  
  let author = $('.article-content .bookinfo li:nth-child(2)').text()
  author = author.substring(3, this.length)

  let extensions = $('.article-content .bookinfo li:nth-child(3)').text()
  extensions = extensions.substring(3, this.length)

  let tag = $('.article-content .bookinfo li:nth-child(4)').text()
  tag = tag.substring(3, this.length)

  let pub_time = $('.article-content .bookinfo li:nth-child(5)').text()
  pub_time = pub_time.substring(3, this.length)

  let score = $('.article-content .bookinfo li:nth-child(6) b').attr('class')
  score = score[score.length - 1]

  const category = $('#mute-category > a').text().trim()

  const brief = $('.article-content').html()

  const book_url = href

  const arr = [book_img, book_name, author, extensions, tag, pub_time, score, category, brief, book_url]
  // console.log(arr)
  
  const strSql = "INSERT INTO books (book_img, book_name, author, extensions, tag, pub_time, score, category, brief, book_url) VALUES (?,?,?,?,?,?,?,?,?,?)"

  conn.query(strSql, arr, (err, results) => {
    if (err) {
      console.log(err)
    }
    console.log(results)
  })
  console.log('Done')
}

// getPageUrl(page)
// const tempBook = 'https://xxx/books/14663.html'
// getBookInfo(tempBook)

for (let i = 1; i <= 328; i++) {
  getPageUrl(i)
}